package com.leonyr.smartipaddemo.crop;

import android.app.Dialog;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;

import com.leonyr.smartipaddemo.R;

public class CompoudChooseFragment extends DialogFragment implements View.OnClickListener{
    private Button btnLeftRight;
    private Button btnUpDown;
    private OnCompoudChooseListener listener;

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        Dialog dialog = new Dialog(getActivity());
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); // 设置Content前设定

        dialog.setCanceledOnTouchOutside(true);
        DisplayMetrics metrics = new DisplayMetrics();
        getActivity().getWindowManager().getDefaultDisplay().getMetrics(metrics);
        int width = metrics.widthPixels;
        int height = metrics.heightPixels;
        Window window = dialog.getWindow();
        if (window != null) {
            WindowManager.LayoutParams lp = window.getAttributes();
            lp.width = ViewGroup.LayoutParams.WRAP_CONTENT;
            lp.height = ViewGroup.LayoutParams.WRAP_CONTENT;
            lp.dimAmount = 0.6f;
            window.setAttributes(lp);
            window.addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        }
        return dialog;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.fm_compoud_choose, container,false);
        initView(view);
        return view;
    }

    private void initView(View view) {
        btnLeftRight = view.findViewById(R.id.btn_leftright);
        btnLeftRight.setOnClickListener(this);
        btnUpDown = view.findViewById(R.id.btn_updown);
        btnUpDown.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.btn_leftright:
                if(listener!=null){
                    listener.onLeftRight();
                    dismiss();
                }
                break;
            case R.id.btn_updown:
                if(listener!=null){
                    listener.onUpDown();
                    dismiss();
                }
                break;
        }
    }

    public interface OnCompoudChooseListener{
        void onLeftRight();
        void onUpDown();

    }

    public void setListener(OnCompoudChooseListener listener) {
        this.listener = listener;
    }
}
